﻿Imports System.Configuration
Imports System.Data.SqlClient
Imports Comercializacion_BE


Public Class metales_preciososADO

    Dim conexion As New SqlConnection

    Public Function ObtenerCodigoMetal() As String

        Dim codigo_metal As String

        conexion.ConnectionString = ConfigurationManager.ConnectionStrings("CnnComercializacion").ConnectionString
        conexion.Open()
        Dim comando As New SqlCommand

        comando.Connection = conexion
        comando.CommandText = "sp_ObtenerCodigoMetal"
        comando.CommandType = CommandType.StoredProcedure

        Dim datos As SqlDataReader
        datos = comando.ExecuteReader()

        While datos.Read()
            codigo_metal = datos("CodigoMetal").ToString()
        End While

        conexion.Close()

        Return codigo_metal

    End Function

    Public Function ObtenerMetalesPreciososGrid() As DataTable

        Dim dt As New DataTable
        Const sql As String = "Select codigometal, simbolo, nombre from metales_preciosos"

        Dim conexion As New SqlConnection(ConfigurationManager.ConnectionStrings("CnnComercializacion").ConnectionString)
        Try
            conexion.Open()
            Using da As New SqlDataAdapter(sql, conexion)
                da.Fill(dt)
            End Using
            conexion.Close()
            Return dt
        Catch myException As Exception
            MsgBox("Error al obtener la informacion de los metales --> " & myException.Message, MsgBoxStyle.Critical)
            Return Nothing
        End Try

    End Function


    Public Function InsertarMetalesPreciosos(ByVal metpreciosos As metales_preciososBE) As Boolean

        'Dim conexion As New SqlConnection

        conexion.ConnectionString = ConfigurationManager.ConnectionStrings("CnnComercializacion").ConnectionString


        Dim comando As New SqlCommand
        comando.Connection = conexion
        comando.CommandText = "sp_InsertarMetalesPreciosos"
        comando.CommandType = CommandType.StoredProcedure

        comando.Parameters.AddWithValue("@codigometal", metpreciosos.codigometal)
        comando.Parameters.AddWithValue("@simbolo", metpreciosos.simbolo)
        comando.Parameters.AddWithValue("@nombre", metpreciosos.nombre)

        Try
            conexion.Open()

            comando.ExecuteScalar()

            Return True
        Catch ex As Exception

            Return False
        Finally
            conexion.Close()
        End Try

    End Function


    Public Function ModificarMetalesPreciosos(ByVal metpreciosos As metales_preciososBE) As Boolean


        conexion.ConnectionString = ConfigurationManager.ConnectionStrings("CnnComercializacion").ConnectionString
        'conexion abierta

        Dim comando As New SqlCommand
        comando.Connection = conexion

        comando.CommandText = "sp_ModificarMetalesPreciosos"
        comando.CommandType = CommandType.StoredProcedure

        comando.Parameters.AddWithValue("@codigometal", metpreciosos.codigometal)
        comando.Parameters.AddWithValue("@simbolo", metpreciosos.simbolo)
        comando.Parameters.AddWithValue("@nombre", metpreciosos.nombre)

        Try
            conexion.Open()

            Return True
        Catch ex As Exception

            Return False
        Finally
            conexion.Close()
        End Try

    End Function



    Public Function EliminarMetalesPreciosos(ByVal metpreciosos As metales_preciososBE) As Boolean


        conexion.ConnectionString = ConfigurationManager.ConnectionStrings("CnnComercializacion").ConnectionString
        'conexion abierta

        Dim comando As New SqlCommand
        comando.Connection = conexion

        comando.CommandText = "sp_EliminarMetalesPreciosos"
        comando.CommandType = CommandType.StoredProcedure

        comando.Parameters.AddWithValue("@codigometal", metpreciosos.codigometal)


        Try
            conexion.Open()

            comando.ExecuteScalar()

            Return True
        Catch ex As Exception

            Return False
        Finally
            conexion.Close()
        End Try


    End Function


End Class
